package com.nstar.sinaweibo.model
{
	import com.nstar.base.util.LoadXMLDelegate;
	import com.nstar.base.util.Logger;
	import com.nstar.base.util.StringUtils;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.geom.Point;
	import flash.net.URLRequest;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Image;
	import mx.controls.Text;
	import mx.core.Container;
	import mx.rpc.IResponder;
	
	import org.bytearray.gif.player.GIFPlayer;
	
	/**
	 * 表情工具加载器
	 * 加载表情配置文件，获取表情列表
	 * 
	 */ 
	public class FaceLoader extends EventDispatcher implements IResponder 
	{
		private var loader:LoadXMLDelegate;
		private var _faceList:ArrayCollection = new ArrayCollection();
		private var _faceType:ArrayCollection = new ArrayCollection();
		
		public var baseUrl:String;
		private var logger:Logger = Logger.getInstance(this);
		
		public function FaceLoader(url:String)
		{
			trace(" faces config url : "+url);
			baseUrl = url;
			loader = new LoadXMLDelegate(this,baseUrl,"object");
			loader.load();
		}
		
		//加载
		public function load():void{
			loader.load();
		}
		
		public function result(data:Object):void{
			trace("load faces ok.");
			baseUrl = String(data.result.faces.url);
			var tmpFaceList:ArrayCollection = data.result.faces.face;
			var tmpFaceType:ArrayCollection = data.result.faces.type.item;
			_faceType.removeAll();
			_faceList.removeAll();
			
			//类型转成VO对象
			for each(var ft:Object in tmpFaceType){
				var ftVO:FaceTypeVO = new FaceTypeVO();
				ftVO.seq = ft.seq;
				ftVO.name = ft.name;
				_faceType.addItem(ftVO);
				
				//获取该类别的表情列表
				var list:ArrayCollection = this.getFaceByType(tmpFaceList,ftVO.name,baseUrl);
				
				var flv:FaceListVO = new FaceListVO();
				flv.type = ftVO.name;
				flv.typeList = list; 
				flv.seq = ftVO.seq;
				_faceList.addItem(flv);
				//trace(ftVO.name+" 共 "+list.length+" 个表情");
			}
			_faceList = new ArrayCollection( _faceList.toArray().sortOn("seq",Array.NUMERIC));
			//trace("路径："+baseUrl+","+tmpFaceList.length+" 个表情，"+tmpFaceType.length+" 个类别");
			this.dispatchEvent(new Event(Event.COMPLETE));
		}		
		
		public function fault(info:Object):void{
			logger.debug("load faces config xml error!!"+info.fault.faultDetail);
		}
		
		//处理表情
		public function filterFaces(textComp:Text,text:String,container:Container,
			xOffset:Number=0,yOffset:Number=0,useGif:Boolean = false):void{
			if(!text) {
				textComp.htmlText = Utils.getStatusHtmlText(text);
				return;
			}

			//先获取表情列表
			var faces:Array = Utils.getFaceList(text);
			//trace("找到 "+faces.length +" 表情.");
			//没有表情显示是就不处理
			if(faces== null || faces.length==0) {
				textComp.htmlText = Utils.getStatusHtmlText(text);
				return;
			}
			//计算每个表情的位置
			var list:Array = new Array();
			var temp:String = text;
			for each(var face:String in faces){
				//检查是否存在于表情列表中
				var f:FaceVO = getFace(face);// 呵呵
				if(f){
					var fullFace:String = "["+f.desc+"]";
					var p:Point = Utils.getCharPoint(textComp,fullFace);
					f.x = p.x+xOffset;
					f.y = p.y+yOffset-3;
					
					list.push(f);
				}
			}
			textComp.text = StringUtils.replace(textComp.text,Utils.REPLACE_CHAR,Utils.whiteSpace(5));
			//trace("处理了 "+list.length +" 表情.");
			textComp.htmlText = Utils.getStatusHtmlText(textComp.text);
			//添加表情
			for each(var item:FaceVO in list){
				var img:Image = new Image();
				img.x = item.x;
				img.y = item.y;
				img.toolTip = item.desc; 
				//如果使用动态表情
				if(useGif){
					var gif:GIFPlayer = new GIFPlayer();
					gif.load(new URLRequest(item.url));
					img.source = gif;
				}else{
					img.source= item.url;
				}
				container.addChild(img);
			}
		}
		
		//清除容器中的表情
		public function removeFaces(container:Container):void{
			//清除表情
			for each(var item:Object in container.getChildren()){
				if(item is Image){
					if(item.getChildAt(0) is GIFPlayer){
						GIFPlayer(item.getChildAt(0)).dispose();
					}
					Image(item).source = null;
					item = null;
				}
			}
		}
		

		//获取指定的表情
		public function getFace(face:String):FaceVO{
			face = face.substr(1,face.length-2);//[嘻嘻]，去掉前后中括号
			for each(var flv:FaceListVO in _faceList){
				for each(var fv:FaceVO in flv.typeList){
					if(fv.desc == face){
						//为了避免同一表情的多次使用时的重复，这里复制一个
						return fv.clone();
					}
				}
			}
			return null;
		}
		
		
		public function get faceList():ArrayCollection{
			return this._faceList;
		}
				
		public function get faceType():ArrayCollection{
			return this._faceType;
		}
		
		//根据类别来获取表情
		private function getFaceByType(list:ArrayCollection,type:String,baseUrl:String):ArrayCollection{
			var result:ArrayCollection = new ArrayCollection();
			for each(var item:Object in list){
				if(item.type == type){
					var faceVO:FaceVO = new FaceVO();
					faceVO.url = baseUrl+item.name;
					faceVO.desc = item.desc;
					faceVO.type = item.type;
					
					result.addItem(faceVO);
				}
			}
			return result;
		}
		
	}
}